package keating;

import java.awt.event.KeyEvent;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;


/**
 *
 * @author Keating
 */
public class TextFieldNoEnter extends javax.swing.JTextArea {
    
    private MainFrame frame;
    
    public TextFieldNoEnter() {
        super();
        this.setLineWrap(true);
        this.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                released(evt);
            }
        });
    }
    
    /**
     *  输入回车时,发送微博
     */
    private void released(java.awt.event.KeyEvent evt) {
        if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
            this.frame.send();
        }
    }

    @Override
    protected Document createDefaultModel() {
        return new IntTextDocument();
    }

    public void setFrame(MainFrame frame) {
        this.frame = frame;
    }
    
    /**
     * 输入回车没有效果
     */
    class IntTextDocument extends PlainDocument {

        public void insertString(int offs, String str, AttributeSet a)
                throws BadLocationException {
            if("\n".equals(str)) {
                return;
            }
            String oldString = getText(0, getLength());
            String newString = oldString.substring(0, offs) + str
                    + oldString.substring(offs);
            super.insertString(offs, str, a);
        }
    }
}
